Welcome to your Support Vector Machine Project! Just follow along with the notebook and instructions below. We will be analyzing the famous iris data set!
For this series of lectures, we will be using the famous Iris flower data set.
The Iris flower data set or Fisher's Iris data set is a multivariate data set introduced by Sir Ronald Fisher in the 1936 as an example of discriminant analysis.
The data set consists of 50 samples from each of three species of Iris (Iris setosa, Iris virginica and Iris versicolor), so 150 total samples. Four features were measured from each sample: the length and the width of the sepals and petals, in centimeters.
Here's a picture of the three different Iris types:
In [17]:
# The Iris Setosa
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg'
Image(url,width=300, height=300)
Out[17]:
In [18]:
# The Iris Versicolor
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/4/41/Iris_versicolor_3.jpg'
Image(url,width=300, height=300)
Out[18]:
In [19]:
# The Iris Virginica
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/9/9f/Iris_virginica.jpg'
Image(url,width=300, height=300)
Out[19]:
The iris dataset contains measurements for 150 iris flowers from three different species.
The three classes in the Iris dataset:
Iris-setosa (n=50)
Iris-versicolor (n=50)
Iris-virginica (n=50)
The four features of the Iris dataset:
sepal length in cm
sepal width in cm
petal length in cm
petal width in cm
Use seaborn to get the iris data by using: iris = sns.load_dataset('iris')
In [24]:
import seaborn as sns
iris = sns.load_dataset('iris')
In [25]:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
Create a pairplot of the data set. Which flower species seems to be the most separable?
In [37]:
# Setosa is the most separable.
sns.pairplot(iris,hue='species',palette='Dark2')
Out[37]:
Create a kde plot of sepal_length versus sepal width for setosa species of flower.
In [44]:
setosa = iris[iris['species']=='setosa']
sns.kdeplot( setosa['sepal_width'], setosa['sepal_length'],
cmap="plasma", shade=True, shade_lowest=False)
Out[44]:
In [45]:
from sklearn.model_selection import train_test_split
In [47]:
X = iris.drop('species',axis=1)
y = iris['species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)
In [48]:
from sklearn.svm import SVC
In [49]:
svc_model = SVC()
In [50]:
svc_model.fit(X_train,y_train)
Out[50]:
In [51]:
predictions = svc_model.predict(X_test)
In [52]:
from sklearn.metrics import classification_report,confusion_matrix
In [53]:
print(confusion_matrix(y_test,predictions))
In [54]:
print(classification_report(y_test,predictions))
Wow! You should have noticed that your model was pretty good! Let's see if we can tune the parameters to try to get even better (unlikely, and you probably would be satisfied with these results in real like because the data set is quite small, but I just want you to practice using GridSearch.
In [55]:
from sklearn.model_selection import GridSearchCV
Create a dictionary called param_grid and fill out some parameters for C and gamma.
In [57]:
param_grid = {'C': [0.1,1, 10, 100], 'gamma': [1,0.1,0.01,0.001]}
Create a GridSearchCV object and fit it to the training data.
In [58]:
grid = GridSearchCV(SVC(),param_grid,refit=True,verbose=2)
grid.fit(X_train,y_train)
Out[58]:
Now take that grid model and create some predictions using the test set and create classification reports and confusion matrices for them. Were you able to improve?
In [59]:
grid_predictions = grid.predict(X_test)
In [60]:
print(confusion_matrix(y_test,grid_predictions))
In [61]:
print(classification_report(y_test,grid_predictions))
You should have done about the same or exactly the same, this makes sense, there is basically just one point that is too noisey to grab, which makes sense, we don't want to have an overfit model that would be able to grab that.